package com.src;

import java.sql.SQLException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
//Path: http://localhost/<appln-folder-name>/register
@Path("/register")
public class Register {
	// HTTP Get Method
	@GET 
	// Path: http://localhost/<appln-folder-name>/register/doregister
	@Path("/doregister")  
	// Produces JSON as response
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") 
	// Query parameters are parameters: http://localhost/<appln-folder-name>/register/doregister?name=pqrs&username=abc&password=xyz
	public String doLogin(@QueryParam("firstName") String firstName,
							@QueryParam("lastName") String lastName,
							@QueryParam("email") String email,
							@QueryParam("username") String uname, 
							@QueryParam("password") String pwd){
		String response = "";
		//System.out.println("Inside doLogin "+uname+"  "+pwd);
		int retCode = registerUser(firstName,lastName,email,uname, pwd);
		if(retCode == 0){
			response = Utitlity.constructJSON("true","");
		}else if(retCode == 1){
			response = Utitlity.constructJSON("false", "You are already registered");
		}else if(retCode == 2){
			response = Utitlity.constructJSON("false", "Special Characters are not allowed in Username and Password");
		}else if(retCode == 3){
			response = Utitlity.constructJSON("false", "Error occured");
		}
		return response;
				
	}
	
	private int registerUser(String firstName,String lastName,String email,String uname, String pwd){
		System.out.println("Inside checkCredentials");
		int result = 3;
		if(Utitlity.isNotNull(uname) && Utitlity.isNotNull(pwd)){
			try {
				if(DBConnection.insertUser(firstName,lastName,email,uname,pwd)){
					System.out.println("RegisterUSer if");
					result = 0;
				}
			} catch(SQLException sqle){
				System.out.println("RegisterUSer catch sqle");
				//When Primary key violation occurs that means user is already registered
				if(sqle.getErrorCode() == 1062){
					result = 1;
				} 
				//When special characters are used in name,username or password
				else if(sqle.getErrorCode() == 1064){
					System.out.println(sqle.getErrorCode());
					result = 2;
				}
			}
			catch (Exception e) {
				result = 3;
			}
		}else{
			result = 3;
		}
			
		return result;
	}
	
}
